// TypeScript file
module Fighter {
    /**
     * 我方防御UI——郭启航
     * 
    */
    export class DefenseUI extends eui.Component {

        constructor() {
            super();
            this.skinName = "resource/assets/PlaneE/DefenseUI.exml"
            this.addEventListener(egret.Event.ADDED_TO_STAGE, this.onAddToStage, this);
            this.addEventListener(egret.Event.REMOVED_FROM_STAGE, this.removeFromStage, this);
            //GameUtil.PlayerMessage.instance.HomePoint = new egret.Point(this.Home.x, this.Home.y);
            this.Hp.maximum = GameUtil.PlayerMessage.instance.BaseHp;
            this.Hp.minimum = 0;

        }
        /**发送血量计时器 */
        public SetHPMessage: egret.Timer = new egret.Timer(3000);
        /**必杀技*/
        public Game_Slay: eui.Image;
        /**基地*/
        private Home: eui.Image;
        /**基地血条 */
        private Hp: eui.ProgressBar;
        /**金币显示*/
        private Gold: eui.BitmapLabel;
        /**加攻击按键*/
        private AddATK: eui.Image;
        /**加血按键*/
        private AddHp: eui.Image;
        /**护盾按键*/
        private Shield: eui.Image
        /**护盾动画*/
        public ObjAnimation: egret.MovieClip = new egret.MovieClip();
        /**必杀动画*/
        public SlayAnimation: egret.MovieClip = new egret.MovieClip();
        /**切换准备按钮*/
        public AddPrepareUI: eui.Group;
        /**切换准备bool*/
        public is_AddPrepareUI: boolean;
        public SlayButtonBool: boolean = false;
        public onAddToStage() {
            this.SetHPMessage.start();
            GameUtil.PlayerMessage.instance.HomeAtk = 1;
            this.addEventListener(egret.Event.ENTER_FRAME, this.Show, this);
            this.AddHp.addEventListener(egret.TouchEvent.TOUCH_TAP, this.AddHpEven, this);
            this.AddATK.addEventListener(egret.TouchEvent.TOUCH_TAP, this.AddATKEven, this);
            this.Game_Slay.addEventListener(egret.TouchEvent.TOUCH_TAP, this.AddSlay, this);
            this.Shield.addEventListener(egret.TouchEvent.TOUCH_TAP, this.AddShield, this)
            this.AddPrepareUI.addEventListener(egret.TouchEvent.TOUCH_TAP, this.AddPrepare, this);
            if (!GameUtil.PlayerMessage.instance.onePerson)
                this.SetHPMessage.addEventListener(egret.TimerEvent.TIMER, this.SetHpMessageEven, this);
        }
        /**向服务器发送基地血量*/
        public SetHpMessageEven() {
            if (!GameUtil.PlayerMessage.instance.onePerson) {
                webNetworkMananger.instance.send_skill(0, GameUtil.PlayerMessage.instance.BaseHp);

            }
        }
        /**切换方法*/
        public AddPrepare() {
            this.is_AddPrepareUI = true;

        }
        private removeFromStage() {
            this.removeEventListener(egret.Event.ENTER_FRAME, this.Show, this);
            /**初始护盾*/
            if (GameUtil.PlayerMessage.instance.HomeShield) {
                this.removeChild(this.ObjAnimation);
                GameUtil.PlayerMessage.instance.HomeShield = false;
            }
            this.SlayButtonBool = false;

        }
        public Show(evt: egret.Event) {
            this.Home.rotation = GameUtil.PlayerMessage.instance.HomeAngle;
            this.Hp.value = GameUtil.PlayerMessage.instance.BaseHp;
            this.Gold.text = GameUtil.PlayerMessage.instance.money.toString();

        }
        /**基地加血*/
        public AddHpEven() {
            if (GameUtil.PlayerMessage.instance.money >= 100) {
                this.AddHp.visible = false;
                if (GameUtil.PlayerMessage.instance.BaseHp == this.Hp.maximum) {
                    this.Hp.maximum += 1;

                }
                GameUtil.PlayerMessage.instance.BaseHp += 1;
                GameUtil.PlayerMessage.instance.money -= 100;
            }
        }
        public AddATKEven() {
            if (GameUtil.PlayerMessage.instance.money >= 200) {
                this.AddATK.visible = false;
                GameUtil.PlayerMessage.instance.HomeAtk += 1;
            }
        }
        /**必杀帧动画*/
        public AddSlay() {
            if (!this.SlayButtonBool) {
                this.SlayButtonBool = true;
                GameUtil.PlayerMessage.instance.HomeSlay = true;
                var objPoint: egret.Point = new egret.Point(50, 100);
                this.SlayAnimation = FrameAnimation.instance.playAnimation(objPoint, "Slay");
                this.addChild(this.SlayAnimation);
                this.SlayAnimation.addEventListener(egret.Event.COMPLETE, this.RemoveAnimation, this);
                webNetworkMananger.getInstance().send_skill(1, 0);
            }
        }
        /**护盾帧动画*/
        public AddShield() {
            if (!GameUtil.PlayerMessage.instance.HomeShield) {
                GameUtil.PlayerMessage.instance.HomeShield = true;

                var PointShield: egret.Point = new egret.Point();
                PointShield.x = 180;
                PointShield.y = 850;
                this.ObjAnimation = FrameAnimation.instance.playAnimation(PointShield, "Shield")
                this.addChild(this.ObjAnimation);
            }
        }
        /**删除动画*/
        public RemoveAnimation() {
            this.removeChild(this.SlayAnimation);
            FrameAnimation.instance.boolPlay = true;
        }
    }
}